package com.summer.dingding.service.impl;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.CorpMessageCorpconversationAsyncsendRequest;
import com.dingtalk.api.response.CorpMessageCorpconversationAsyncsendResponse;
import com.summer.dingding.util.DingDingConstants;
import com.summer.dingding.util.DingDingUtil;
import com.taobao.api.ApiException;

/**
* @Description:对接钉钉的工具类
* @Authod:zhang_cq
* @Date:2018/3/19 下午2:59
*/
public class DingDingServiceImpl1 {
    private static String CORPID = DingDingConstants.Config.CORPID; // 企业Id
    private static String CORPSECRET = DingDingConstants.Config.CORPSECRET; // 企业应用的凭证密钥
    public static Long AGENTID = 206674255l; // 自动分配微应用的ID

    public static JSONObject sendDDMessage(Long agentId, String userId, String msgcontent) {
        DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");
        CorpMessageCorpconversationAsyncsendRequest req = new CorpMessageCorpconversationAsyncsendRequest();
        req.setMsgtype("oa"); // 消息类型
        req.setAgentId(agentId);
        req.setUseridList(userId);
        req.setToAllUser(false); // 是否发送给企业全部用户
        req.setMsgcontentString(msgcontent);
        try {
            String token = DingDingUtil.getAppAccesstoken();
            CorpMessageCorpconversationAsyncsendResponse rsp = client.execute(req, token);
            JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(rsp.getResult()));
            if (json != null) {
                return json;
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }


    /** 
    * @Description:根据部门ID获取用户信息
    * @Method:getDDUsersByDeptId
    * @Authod:zhang_cq 
    * @Date:2018/3/20 上午11:19
     * @param departmentId
    */
//    public static List<Users> getDDUsersByDeptId(Long departmentId){
//        try {
//            String accessToken = getToken(CORPID,CORPSECRET);
//            String result = ClientUtil.sendGet("https://oapi.dingtalk.com/user/list?access_token="+accessToken+"&department_id=" + departmentId);
//            JSONObject json = JSONObject.fromObject(result);
//            if (json != null && "0".equals(json.get("errcode").toString())) {
//                JSONArray userlist = JSONArray.fromObject((json.get("userlist")));
//                if (userlist != null && userlist.size() > 0) {
//                    List<Users> userList = new ArrayList<Users>();
//                    for(int i=0;i<userlist.size();i++){
//                        JSONObject user = userlist.getJSONObject(i);
//                        String mobile = user.getString("mobile");
//                        String userId = user.getString("userid");
//                        String name = user.getString("name");
//                        Users users = new Users();
//                        if(!StringUtils.isBlank(mobile)){
//                            users.setMobile(mobile);
//                            users.setDDUserId(userId);
//                            users.setRealName(name);
//                            userList.add(users);
//                        }
//                    }
//                    return userList;
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    /**
     * @Description:根据部门ID获取用户信息(userid和name)
     * @Method:getDDSimpleUsersByDeptId
     * @Authod:zhang_cq
     * @Date:2018/3/20
     * @param accessToken
     * @param departmentId
     */
//    public static void getDDSimpleUsersByDeptId(String accessToken, Long departmentId){
//        try {
//            String result = ClientUtil.sendGet("https://oapi.dingtalk.com/user/simplelist?access_token="+accessToken+"&department_id=" + departmentId);
//            JSONObject json = JSONObject.fromObject(result);
//            if (json != null && "0".equals(json.get("errcode").toString())) {
//                JSONArray userlist = JSONArray.fromObject((json.get("userlist")));
//                if (userlist != null && userlist.size() > 0) {
//                    for(int i=0;i<userlist.size();i++){
//                        JSONObject user = userlist.getJSONObject(i);
//                        String name = user.getString("name");
//                        String userId = user.getString("userid");
//                        System.out.println(name + ":" + userId);
//                    }
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /** 
    * @Description:获取员工的打卡记录
    * @Method:getRecordInfo
    * @Authod:zhang_cq 
    * @Date:2018/3/21 下午5:42
     * @param checkDateFrom 查询考勤打卡记录的起始工作日
     * @param checkDateTo 查询考勤打卡记录的结束工作日。注意，起始与结束工作日最多相隔7天
    */
//    private static String getRecordInfo(String userId, String checkDateFrom, String checkDateTo){
//        try {
//            String token = getToken(CORPID,CORPSECRET);
//            Map<String, String> data = new HashMap<String, String>();
//            data.put("userIds","["+userId+"]");
//            data.put("checkDateFrom",checkDateFrom);
//            data.put("checkDateTo",checkDateTo);
//            JSONObject dataJson = JSONObject.fromObject(data);
//            String result = sendHttpPost("https://oapi.dingtalk.com/attendance/listRecord?access_token="+token,dataJson.toString());
//            System.out.println(result);
//            JSONObject json = JSONObject.fromObject(result);
//            if (json != null && "0".equals(json.get("errcode").toString())) {
//                JSONArray recordresult = JSONArray.fromObject((json.get("recordresult")));
//                if (recordresult != null && recordresult.size() > 0) {
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public static String sendHttpPost(String url, String body) throws Exception {
        String result = null;
        HttpClient client = HttpClients.createDefault();
        URIBuilder builder = new URIBuilder();
        try {

            HttpPost post = new HttpPost(url);
            //设置请求头
            post.setHeader("Content-Type", "application/json");
            //设置请求体
            post.setEntity(new StringEntity(body));
            //获取返回信息
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                entity = new BufferedHttpEntity(entity);

                InputStream in = entity.getContent();
                byte[] read = new byte[1024];
                byte[] all = new byte[0];
                int num;
                while ((num = in.read(read)) > 0) {
                    byte[] temp = new byte[all.length + num];
                    System.arraycopy(all, 0, temp, 0, all.length);
                    System.arraycopy(read, 0, temp, all.length, num);
                    all = temp;
                }
                result = new String(all,"UTF-8");
                if (null != in) {
                    in.close();
                }
            }
            return result;
        } catch (Exception e) {
            System.out.println("接口请求失败"+e.getStackTrace());
        }
        return result;
    }

    public static String doPost(String url, JSONObject jsonObject, String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            StringEntity entity = new StringEntity(jsonObject.toString(),charset);
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            HttpResponse response = httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }

    /**
    * @Description:销售是否上班打卡
    * @Method:getSignInInfo
    * @Authod:zhang_cq
    * @Date:2018/3/22 上午10:08
    */
//    public static Boolean getSignInInfo(String userId){
//        String token = getToken(CORPID,CORPSECRET);
//        String recordUrl = "https://oapi.dingtalk.com/attendance/list?access_token=" + token;
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("workDateFrom","2018-03-21 06:00:00");
//        jsonObject.put("workDateTo","2018-03-21 19:00:00");
//        List<String> usersList = new ArrayList<String>();
//        usersList.add(userId);
//        jsonObject.put("userIdList",usersList);
//        jsonObject.put("offset",0);
//        jsonObject.put("limit",1);
//        String result = doPost(recordUrl,jsonObject,"utf-8");
//        JSONObject resutJSON = jsonObject.fromObject(result);
//        String msg = (String)resutJSON.get("errmsg");
//        if("ok".equals(msg)){
//            JSONArray jsonArray = JSONArray.fromObject(resutJSON.get("recordresult"));
//            if (jsonArray != null && jsonArray.size() > 0) {
//                JSONObject recordInfo = JSONObject.fromObject(jsonArray.get(0));
//                String checkType = recordInfo.get("checkType").toString();
//                if("OnDuty".equals(checkType)){
//                    return true;
//                }
//                //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time))); // 上班打卡时间
//            }
//        }
//        return false;
//    }
    // 测试主方法
    public static void main(String[] args) {
    	
//    	int pre = new Double(Math.random() * 993).intValue() + 1;
//    	DingDingMessageTemplate message = new DingDingMessageTemplate();
//		message.setMessage_url("http://www.baidu.com");
//		System.out.println(message.getHead().getBgcolor());
//		message.getBody().setContent("你好中国人");
//		message.getBody().setTitle("吃饭了！");
//		message.getBody().setAuthor("老夏");
//		message.getBody().setImage("http://192.168.31.223:8080/include/images/emoticon/" + pre + ".jpg");
//		Map<String, String> form1 = new HashMap<String, String>();
//		form1.put("key", "午餐一：");
//		form1.put("value", "牛肉炖柿子");
//		Map<String, String> form2 = new HashMap<String, String>();
//		form2.put("key", "午餐二：");
//		form2.put("value", "萝卜呵呵哒");
//		message.getBody().getForm().add(form1);
//		message.getBody().getForm().add(form2);
    	Map<String, String>[] maps = new HashMap[2];
    	Map<String, String> form1 = new HashMap<String, String>();
		form1.put("key", "午餐一：");
		form1.put("value", "牛肉炖柿子");
		Map<String, String> form2 = new HashMap<String, String>();
		form2.put("key", "午餐二：");
		form2.put("value", "萝卜呵呵哒");
		maps[0]=form1;
		maps[1]=null;
    	
    	
    	
//		DingDingMessageTemplate message = DingDingMessageServiceImpl.buildSendMessage("https://www.xihuanqian.com", "吃饭了！", "按时吃饭么么哒~", "老夏", maps);
        //String msgcontent = "{\"message_url\": \"http://dingtalk.com\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"头部标题\"},\"body\": {\"title\": \"正文标题\",\"form\": [{\"key\": \"姓名:\",\"value\": \"张三\"},{\"key\": \"爱好:\",\"value\": \"打球、听音乐\"}],\"rich\": {\"num\": \"15.6\",\"unit\": \"元\"},\"content\": \"大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本\",\"image\": \"@lADOADmaWMzazQKA\",\"file_count\": \"3\",\"author\": \"李四 \"}}";
//        String msgcontent = "{\"message_url\": \"http://119.48.242.122:8080\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"头部标题\"},\"body\": {\"title\": \"午饭时间就要到了！\",\"form\": [{\"key\": \"今日午餐:\",\"value\": \"黑白菜+卤水大豆腐\"}],\"content\": \"按时吃饭，别饿懵逼了！\",\"image\": \"http://upload-images.jianshu.io/upload_images/763193-c46dd76b34b3d1cb.JPG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240\",\"author\": \"老夏 \"}}";
        String msgcontent = "{\"message_url\": \"http://dingtalk.com\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"头部标题\"},\"body\": {\"title\": \"正文标题\",\"form\": [{\"key\": \"姓名:\",\"value\": \"张三\"},{\"key\": \"爱好:\",\"value\": \"打球、听音乐\"}],\"rich\": {\"num\": \"15.6\",\"unit\": \"元\"},\"content\": \"大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本\",\"image\": \"@lADOADmaWMzazQKA\",\"file_count\": \"3\",\"author\": \"李四 \"}}";
//        JSONObject json = sendDDMessage(AGENTID,"manager4697",JSONObject.toJSONString(message));
//        System.out.println(json);
//        getSignInInfo("071323270524189541");
    }
}
